library(ltm)

CalibrationData <- read.csv("calibration.csv", header=T, row.names="response_id")
CalibrationData <- CalibrationData[,-1] # Remove first useless column
CalibrationData <- CalibrationData[,colMeans(is.na(CalibrationData))!=1] # remove empty columns

# Recode NAs to 0s for knowledge questions
for (i in c(1583:1588,1590:1629, 1631:1651)){
  CalibrationData[is.na(CalibrationData[,paste("question_", i, sep="")]),paste("question_", i, sep="")] <- 0
  CalibrationData[(CalibrationData[,paste("question_", i, sep="")]==-1), paste("question_", i, sep="")] <- 0
}
use.these <- c(1583:1588,1590:1598, 1600:1620, 1622:1629, 1631:1651)

# Subset for IRT analysis
IRTData <- CalibrationData[,paste("question_", use.these, sep="")]

# Compute the item parameters
ltm.res = ltm(IRTData~z1, IRT.param=TRUE)
results = data.frame(coef(ltm.res))
results = results[order(results$Dffclt), ]

# results contains the CAT parameters for the associated question.  These are listed in Table 2
print(results)
